import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {getInfo, login, logout, phoneLogin} from '@/api/login'
import {getToken, removeToken, setToken} from '@/utils/token'
import images from '@/utils/images'
import {smsLogin} from "@/api/login";
const baseUrl = config.baseUrl

const user = {
    state: {
        token: getToken(),
        username: storage.get(constant.username),
        nickname: storage.get(constant.nickname),
        avatar: storage.get(constant.avatar),
        userId: storage.get(constant.userId),
        roles: storage.get(constant.roles),
        permissions: storage.get(constant.permissions),
        tabbar: storage.get(constant.tabbar),
        loginDate: storage.get(constant.loginDate)

    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_USERNAME: (state, username) => {
            state.username = username
            storage.set(constant.username, username)
        },
        SET_NICKNAME: (state, nickname) => {
            state.nickname = nickname
            storage.set(constant.nickname, nickname)
        },
        SET_USERID: (state, userId) => {
            state.userId = userId
            storage.set(constant.userId, userId)
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
            storage.set(constant.avatar, avatar)
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
            storage.set(constant.roles, roles)
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
            storage.set(constant.permissions, permissions)
        },
        SET_TABBAR: (state, tabbar) => {
            state.tabbar = tabbar
            storage.set(constant.tabbar, tabbar)
        },
        SET_LOGIN_DATE: (state, loginDate) => {
            state.loginDate = loginDate
            storage.set(constant.loginDate, loginDate)
        },
    },

    actions: {
        // 登录
        Login({commit}, loginInfo) {
            let username = null
            if (loginInfo.username != null) {
                username = loginInfo.username.trim()
            }
            let mobile = loginInfo.mobile
            const password = loginInfo.password
            const code = loginInfo.code
            const uuid = loginInfo.uuid
            return new Promise((resolve, reject) => {
                login(username, password, code, uuid, mobile).then(res => {
                    setToken(res.token)
                    commit('SET_TOKEN', res.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        PhoneLogin({commit}, phone) {
            return new Promise((resolve, reject) => {
                phoneLogin(phone).then(res => {
                    setToken(res.token)
                    commit('SET_TOKEN', res.token)
                    commit('SET_TABBAR', res.tabbar)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        GetInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const user = res.user
                    let nickname = user.nickName
                    let userId = user.userId
                    const avatar = (user.avatar === "" || user.avatar == null) ? require(images.avatar) : user.avatar
                    const username = (user.userName === "" || user.userName == null) ? "" : user.userName
                    if (res.roles && res.roles.length > 0) {
                        commit('SET_ROLES', res.roles)
                        commit('SET_PERMISSIONS', res.permissions)
                    } else {
                        commit('SET_ROLES', ['ROLE_DEFAULT'])
                    }
                    commit('SET_USERNAME', username)
                    commit('SET_USERID', userId)
                    commit('SET_NICKNAME', nickname)
                    commit('SET_AVATAR', avatar)
                    commit('SET_LOGIN_DATE', user.loginDate)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        Logout({commit, state}) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', [])
                    commit('SET_PERMISSIONS', [])
                    commit('SET_AVATAR', '')
                    removeToken()
                    storage.clean()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default user
